package com.zut.wx.controller;

import com.zut.wx.handler.MsgHandlerFactory;
import com.zut.wx.handler.WxChatMsgHandle;
import com.zut.wx.until.MessageUtil;
import com.zut.wx.until.SHA1;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@RestController
@Log4j2
public class CallBackController {

    @Resource
    private MsgHandlerFactory msgHandlerFactory;

    private String token ="wasdsdfasvav";

    @RequestMapping("/test")
    public String test() {
        return "hello";
    }

    //回调消息校验
    @GetMapping("/callBack")
    public String callBack(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {

        log.info("get验签请求参数：signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr);
        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        if (signature.equals(shaStr)){
            return echostr;
        }
        return "unknown";
    }

    //回调消息校验
    @PostMapping(value = "/callBack",produces ="application/xml;charset=UTF-8" )
    public String callBack(
                           @RequestBody String requestBody,
                           @RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam(value = "msg_signature",required = false) String msgSignature) {

        log.info("接受到微信请求参数：requestBody:{},signature:{},timestamp:{},nonce:{},echostr:{}",requestBody, signature, timestamp, nonce);
        Map<String, String> messageMap = MessageUtil.parseXml(requestBody);
        String fromUserName = messageMap.get("FromUserName");
        String toUserName = messageMap.get("ToUserName");
        String msgType = messageMap.get("MsgType");
        String event = messageMap.get("Event");
        String build;
        if (!StringUtils.isEmpty(event)){
             build=msgType+"."+event;

        }else {
             build=msgType;

        }
        WxChatMsgHandle handle = msgHandlerFactory.getHandleByType(build);
        if (Objects.isNull(handle)){
            return null;
        }
        String replyContent = handle.dealMsg(messageMap);
        return replyContent;



//        String content = "<xml>\n" +
//                "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
//                "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//                "  <CreateTime>12345678</CreateTime>\n" +
//                "  <MsgType><![CDATA[text]]></MsgType>\n" +
//                "  <Content><![CDATA[我叫打胶威]]></Content>\n" +
//                "</xml>";
//        return content;
    }
}
